﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SAD_SiS.Models;
using SAD_SiS.ViewModels;

namespace SAD_SiS.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        private int PageSize = 6;
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Search(FormCollection form)
        {
            int page = 1;
            string key = form["SearchKey"];
            string type = form["SearchType"];

            List<V_Product> productList = null;
            List<Shop> shopList = null;
            SearchView returnView = new SearchView();
            if (type.Equals("Product"))
            {
                using (SADEntities dbContext = new SADEntities())
                {
                    productList = (from p in dbContext.V_Product where p.Active == true && p.Name.Contains(key) select p).ToList();
                }
                returnView.ProductList = productList.Skip((page - 1) * PageSize).Take(PageSize).ToList();
                returnView.TotalItem = productList.Count;
            }
            else
            {
                using (SADEntities dbContext = new SADEntities())
                {
                    shopList = (from p in dbContext.Shops where p.Active == true && p.ShopName.Contains(key) select p).ToList();
                }
                returnView.ShopList = shopList.Skip((page - 1) * PageSize).Take(PageSize).ToList();
                returnView.TotalItem = shopList.Count;
            
            }

            returnView.ItemsPerPage = PageSize;
            returnView.CurrentPage = page;
            returnView.TotalPage = (int)Math.Ceiling((decimal)returnView.TotalItem / returnView.ItemsPerPage);
            ViewBag.SearchKey = key;
            ViewBag.SearchType = type;
            return View(returnView);
        }

        public ActionResult Search(string str, string type, int page=1)
        {
            string key = str;

            List<V_Product> productList = null;
            List<Shop> shopList = null;
            SearchView returnView = new SearchView();
            if (type.Equals("Product"))
            {
                using (SADEntities dbContext = new SADEntities())
                {
                    productList = (from p in dbContext.V_Product where p.Active == true && p.Name.Contains(key) select p).ToList();
                }
                returnView.ProductList = productList.Skip((page - 1) * PageSize).Take(PageSize).ToList();
                returnView.TotalItem = productList.Count;
            }
            else
            {
                using (SADEntities dbContext = new SADEntities())
                {
                    shopList = (from p in dbContext.Shops where p.Active == true && p.ShopName.Contains(key) select p).ToList();
                }
                returnView.ShopList = shopList.Skip((page - 1) * PageSize).Take(PageSize).ToList();
                returnView.TotalItem = shopList.Count;

            }

            returnView.ItemsPerPage = PageSize;
            returnView.CurrentPage = page;
            returnView.TotalPage = (int)Math.Ceiling((decimal)returnView.TotalItem / returnView.ItemsPerPage);
            ViewBag.SearchKey = key;
            ViewBag.SearchType = type;
            return View(returnView);
        }
    }
}
